﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebApp.Companys.Dtos;
using WebApp.Navigations.Dtos;

namespace WebApp.Helper
{
    /// <summary>
    /// 泛型递归求树形结构
    /// </summary>
    public static class RecursionTreeHelper
    {
        public static void LoopToAppendChildren(List<PermissionTreeDto> all, PermissionTreeDto curItem, Guid? pid, bool needbtn)
        {

            var subItems = all.Where(ee => ee.Pid == curItem.value).ToList();

    
        

         
            if (subItems.Count > 0)
            {
                curItem.children = new List<PermissionTreeDto>();
                curItem.children.AddRange(subItems);
            }
            else
            {
                curItem.children = new List<PermissionTreeDto>();
            }

        

            foreach (var subItem in subItems)
            {
                if (subItem.value == pid && ((pid.ToString().Trim() != "" || pid.HasValue)))
                {
                    //subItem.disabled = true;//禁用当前节点
                }
                LoopToAppendChildren(all, subItem, pid, needbtn);
            }
        }



        public static void LoopNaviBarAppendChildren(List<NavigationBarDto> all, NavigationBarDto curItem)
        {

            var subItems = all.Where(ee => ee.pid == curItem.id).ToList();

            if (subItems.Count > 0)
            {
                curItem.children = new List<NavigationBarDto>();
                curItem.children.AddRange(subItems);
            }
            else
            {
                curItem.children = new List<NavigationBarDto>();
            }


            foreach (var subItem in subItems)
            {
                LoopNaviBarAppendChildren(all, subItem);
            }
        }
        public static void LoopChildrenUiMenuTree(List<UiMenuTreeDto> all, UiMenuTreeDto curItem)
        {

            var subItems = all.Where(ee => ee.Pid == curItem.Id).ToList();

            if (subItems.Count > 0)
            {
                curItem.children = new List<UiMenuTreeDto>();
                curItem.children.AddRange(subItems);
            }
            else
            {
                curItem.children = null;
            }


            foreach (var subItem in subItems)
            {
                LoopChildrenUiMenuTree(all, subItem);
            }
        }
        public static void LoopChildrenCompanyTree(List<CompanyTreeDto> all, CompanyTreeDto curItem)
        {

            var subItems = all.Where(ee => ee.Pid == curItem.Id).ToList();

            if (subItems.Count > 0)
            {
                curItem.children = new List<CompanyTreeDto>();
                curItem.children.AddRange(subItems);
            }
            else
            {
                curItem.children = null;
            }


            foreach (var subItem in subItems)
            {
                LoopChildrenCompanyTree(all, subItem);
            }
        }


        public static void LoopToAppendChildrenT<T>(List<T> all, T curItem, string parentIdName = "Pid", string idName = "value", string childrenName = "children")
        {
            var subItems = all.Where(ee => ee.GetType().GetProperty(parentIdName).GetValue(ee, null).ToString() == curItem.GetType().GetProperty(idName).GetValue(curItem, null).ToString()).ToList();

            if (subItems.Count > 0) curItem.GetType().GetField(childrenName).SetValue(curItem, subItems);
            foreach (var subItem in subItems)
            {
                LoopToAppendChildrenT(all, subItem);
            }
        }
    }
}
